class VSlider
{
  int barWidth, barHeight; // width and height of bar. NOTE: barWidth also used as slider button height.
  int Xpos, Ypos;          // upper-left position of bar
  float Spos, newSpos;     // y (upper) position of slider
  int SposMin, SposMax;    // max and min values of slider
  boolean over;            // True if hovering over the Slider
  boolean locked;          // True if a mouse button is pressed while on the Slider
  int roundRad = 5;             // radius of rounded rectangle for thumb

  ColorScheme cs = global_cs;
  color barOutlineCol  = cs.bar_outline;
  color barFillCol     = cs.bar_background;
  color sliderFillCol  = cs.slider_fill;
  color barHoverCol    = cs.bar_hover;
  color sliderPressCol = cs.slider_press;


  VSlider (int X_start, int Y_start, int bar_width, int bar_height) {
    barWidth = bar_width;
    barHeight = bar_height;

    Xpos = X_start;
    Ypos = Y_start;
    Spos = Ypos + barHeight/2 - barWidth/2; // center it initially
    newSpos = Spos;
    SposMin = Ypos;
    SposMax = Ypos + barHeight - barWidth;
  }

  void update() {
    over = overit();
    if (mousePressed && over) locked = true; 
    else locked = false;

    if (locked) {
      newSpos = constrain(mouseY-barWidth/2, SposMin, SposMax);
    }
    Spos = newSpos;
  }

  // Return true if mouse is over thumb of slider
  boolean overit() {
    if (mouseX > Xpos && mouseX < Xpos+barWidth &&
      mouseY > Ypos && mouseY < Ypos+barHeight) {
      return true;
    } 
    else {
      return false;
    }
  }


  void display() {
    stroke(barOutlineCol);
    fill(barFillCol);
    rect(Xpos, Ypos, barWidth, barHeight);
    if (over) {
      fill(barHoverCol);
    }
    if (locked) {
      fill(sliderPressCol);
    }
    if (!over && !locked) {
      fill (sliderFillCol);
    }
    if (abs(Spos-newSpos)>0.1) fill (sliderPressCol);
    rect(Xpos, Spos, barWidth, barWidth,roundRad);
  }

  int getValue() {
    // Convert slider position Spos to a value between 0 and 1
    return ((int)Spos-Ypos);
  }

  // set the value of this slider
  void setValue(float value) {
    // convert a value (0 to 1) to slider position Spos
    if (value<0) value=0.0;
    if (value>1) value=1.0;
    Spos = Ypos + ((barHeight-barWidth)*value);
    newSpos = Spos;
  }
}
